package com.hqyj.zss.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.zss.dao.AdminDao;
import com.hqyj.zss.pojo.Admin;
import com.hqyj.zss.pojo.Customer;
import com.hqyj.zss.pojo.Employee;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.AdminService;
import com.hqyj.zss.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private MD5Util md5Util;
    @Override
    public int addAdmin(Admin admin) {
        /* MD5散列算法加密  密码*/
        admin.setA_password(md5Util.md5Hash(admin.getA_password(),admin.getA_username()));
        return adminDao.addAdmin(admin);
    }

    @Override
    public PageData<Admin> queryAllAdmin(int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Admin> userList = adminDao.queryAllAdmin();
        PageInfo<Admin> pageInfo = new PageInfo<>(userList);
        PageData<Admin> pageData = new PageData<>();
        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);

        if(pageInfo.isHasNextPage()){
            pageData.setNextPage(pageNumber + 1);
        }else{
            pageData.setNextPage(pageNumber);
        }
        if(pageInfo.isHasPreviousPage()){
            pageData.setPreviousPage(pageNumber - 1);
        }else{
            pageData.setPreviousPage(pageNumber);
        }
        pageData.setTotalPage(pageInfo.getPages());
        pageData.setTotalSize((int)pageInfo.getTotal());
        pageData.setList(pageInfo.getList());
        return pageData;
    }


    @Override
    public Admin getOneByUserName(String username) {
        Admin admin = adminDao.getOneCustomerByUsername(username);
        return admin;
    }

    /**
     * 使用shiro框架实现登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Map<String, Object> adminLogin(String username, String password) {
        Map<String,Object> result = new HashMap<>();
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断当前用户是否已经认证过
        if (!subject.isAuthenticated()){
            //创建一个认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //做登陆
            try{
                subject.login(token);
            }catch(UnknownAccountException e){
                //未知账号异常
                result.put("code",-1);
                result.put("message",username+"用户不存在");
                return result;
            }catch (IncorrectCredentialsException e){
                //密码错误
                result.put("code",-2);
                result.put("message",username+"密码错误");
                return result;
            }catch (AuthenticationException e){
                result.put("code",-10);
                result.put("message","认证失败");
                return result;
            }
        }
        //认证已通过，获取shiro session域中的用户信息
        Session session = subject.getSession();
        Object loginUser = session.getAttribute("loginUser1");

        result.put("code",0);
        result.put("message",username+"认证成功");
        //直接返回给控制器方法
        result.put("loginUser",loginUser);

        return result;
    }

    /**
     * 实现登出
     * @return
     */
    @Override
    public Map<String, Object> adminLogout() {
        //使用shiro框架获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //使用shiro框架对当前用户登出
        subject.logout();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","成功登出");
        return result;
    }

}
